package pers.cz.postgirl.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @program: postgirl-collection-client
 * @description: MybatisSqlSessionFactory
 * @author: Cheng Zhi
 * @create: 2022-03-11 15:28
 **/
public class MybatisSqlSessionFactory {

    private SqlSessionFactory sqlSessionFactory;

    private static MybatisSqlSessionFactory mybatisSqlSessionFactory;
    public synchronized static MybatisSqlSessionFactory getMybatisSqlSessionFactory() {

        if (mybatisSqlSessionFactory == null) {
            mybatisSqlSessionFactory = new MybatisSqlSessionFactory();
        }
        return mybatisSqlSessionFactory;
    }

    public synchronized SqlSessionFactory getSqlSessionFactory(){

        if (sqlSessionFactory == null) {
            try {
                // 1、获取配置文件
                InputStream inputStream = Object.class.getResourceAsStream("/mybatis-config.xml");
                // 2、构建SqlSessionFactory
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            } catch (Exception e) {
                System.out.println("-------------配置mybatis文件失败--------" + e.getMessage());
            }
        }

        return sqlSessionFactory;
    }
}
